package pos.gengshen.com.gspos.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import pos.gengshen.com.gspos.R;
import pos.gengshen.com.gspos.common.Constant;
import pos.gengshen.com.gspos.utils.DateTimeUtilT;
import pos.gengshen.com.gspos.utils.HttpUtils;

/**
 * Created by Administrator on 2018/7/9/009.
 */



public class WorkActivity extends AppCompatActivity{

    private Button fastShift, exit;
    private TextView startTv,endTv;
    private TableLayout tableLayout;
    private TextView income,orderTotal,discount,orderNum,refundMoney;
    private TextView refendNum,totalIntagral,orderover,paycordNum,paycodeTotal;
    String url = Constant.SERVER_URL+"/admin/shift/getPrintShiftData";  //数据的url
    private Intent intent1;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_work);
        fastShift = (Button)findViewById(R.id.fastShift);
        exit = (Button)findViewById(R.id.exit);
        startTv = (TextView)findViewById(R.id.startDate);
        endTv = (TextView)findViewById(R.id.endDate);
        endTv.setText(DateTimeUtilT.dateTime(new Date()));
        income = (TextView)findViewById(R.id.income);
        orderTotal = (TextView)findViewById(R.id.orderTotal);
        orderNum = (TextView)findViewById(R.id.orderNum);
        discount = (TextView)findViewById(R.id.discount);
        orderNum = (TextView)findViewById(R.id.orderNum);
        refundMoney = (TextView)findViewById(R.id.refundMoney);
        refendNum  = (TextView)findViewById(R.id.refendNum);
        tableLayout = (TableLayout)findViewById(R.id.tableview1);
        totalIntagral =(TextView)findViewById(R.id.totalIntagral);
        orderover =(TextView)findViewById(R.id.orderover);
        paycordNum = (TextView)findViewById(R.id.payRecodeNum);
        paycodeTotal = (TextView)findViewById(R.id.paycodeTotal);
        //获取传递过来的参数
        Intent intent = getIntent();
        String responseText = intent.getStringExtra("shift");
        if(responseText != null){
            JSONObject object = JSON.parseObject(responseText);
            String code = object.getString("code");
            if(code.equals("200")){
                System.out.println("成功获取数据=======");
                JSONObject data = object.getJSONObject("obj");
                Date start = data.getDate("stratDate");
                //设置上班时间
                startTv.setText(DateTimeUtilT.dateTime(start));
                Double d1=data.getInteger("income")/100.00;
                income.setText(String.valueOf(d1));
                Double d2 = data.getInteger("totalMoney")/100.00;
                orderTotal.setText(String.valueOf(d2));
                orderNum.setText(data.getString("orderTotal"));
                Double d3 = data.getInteger("discount")/100.0;
                discount.setText(String.valueOf(d3));
                Double d4 = data.getInteger("refund")/100.0;
                refundMoney.setText(String.valueOf(d4));
                refendNum.setText(data.getString("refundNum"));
                totalIntagral.setText(data.getString("totalIntagral"));
                orderover.setText(data.getString("orderOverNum"));
                paycordNum.setText(data.getString("payOrderNum"));
                Double d6 = data.getInteger("paycordTotal")/100.00;
                paycodeTotal.setText(String.valueOf(d6));
                String shiftId = data.getString("id");
                url = url +"?shiftId="+27;
                intent1 = new Intent(WorkActivity.this,ShiftActivity.class);
                intent1.putExtra("shiftId",shiftId);
            }
        }




        fastShift.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                HttpUtils.doGet(url, null, null, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(WorkActivity.this,"网络访问出错",Toast.LENGTH_SHORT).show();
                            }
                        });

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        final String result = response.body().string();
                        intent1.putExtra("shiftDetail",result);
                        startActivity(intent1);
                    }
                });









            }
        });

        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WorkActivity.this.finish();

            }
        });






    }
}
